﻿// RotationAxisParameters
// Container for RotationAxis parameters
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.IO;
using System.Xml;
using System.Globalization;
using log4net;

namespace FLMScan.Parameters
{
    class RotationAxisParameters
    {
        private ILog logger;

        private const double DefaultX = 0.0;
        private const double DefaultY = 1.0;
        private const double DefaultZ = 0.0;
        
        private double x;
        private double y;
        private double z;

        private const String RotationAxisNormalXParameter = "RotationAxisNormalX";
        private const String RotationAxisNormalYParameter = "RotationAxisNormalY";
        private const String RotationAxisNormalZParameter = "RotationAxisNormalZ";           

        public RotationAxisParameters(string configurationFilePath)
        {
            logger = LogManager.GetLogger(typeof(RotationAxisParameters));
            Initialize(configurationFilePath);            
        }

        public void Serialize(XmlTextWriter textWriter)
        {
            FLMScanParameters.SerializeParameter(textWriter, RotationAxisNormalXParameter, X.ToString());
            FLMScanParameters.SerializeParameter(textWriter, RotationAxisNormalYParameter, Y.ToString());
            FLMScanParameters.SerializeParameter(textWriter, RotationAxisNormalZParameter, Z.ToString());            
        }

        private void Initialize(string configurationFilePath)
        {
            SetDefaultValues();

            try
            {
                TextReader reader = new StreamReader(configurationFilePath);
                GetConfigValues(reader);
            }
            catch (FileNotFoundException)
            {

            }
        }

        private void SetDefaultValues()
        {
            x = DefaultX;
            y = DefaultY;
            z = DefaultZ;
        }        

        private void GetConfigValues(TextReader reader)
        {
            XmlTextReader xmlReader = new XmlTextReader(reader);
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element &&
                    xmlReader.Name.Equals("add") &&
                    xmlReader.MoveToNextAttribute())
                {
                    try
                    {
                        switch (xmlReader.Name)
                        {
                            case RotationAxisNormalXParameter:
                                string value = xmlReader.Value;
                                string ensureDecimalPoints = value.Replace('.', ',');
                                X = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, RotationAxisNormalXParameter, X);
                                break;
                            case RotationAxisNormalYParameter:
                                value = xmlReader.Value;
                                ensureDecimalPoints = value.Replace('.', ',');
                                Y = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, RotationAxisNormalYParameter, Y);
                                break;
                            case RotationAxisNormalZParameter:
                                value = xmlReader.Value;
                                ensureDecimalPoints = value.Replace('.', ',');
                                Z = double.Parse(ensureDecimalPoints, NumberStyles.Float);
                                logger.DebugFormat(FLMScanParameters.LogSettingParameterToValue, RotationAxisNormalZParameter, Z);
                                break;
                        }
                    }
                    catch (System.Exception e)
                    {
                        System.Diagnostics.Debug.Assert(false, e.ToString());
                        logger.Error(FLMScanParameters.LogExceptionReadingParametersFromConfigFile, e);
                    }
                }
            }

            xmlReader.Close();
        }

        public double X
        {
            get { return x; }
            set { x = value; }
        }

        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        public double Z
        {
            get { return z; }
            set { z = value; }
        }
    }
}
